package com.msb.c06;

import java.util.concurrent.TimeUnit;

/**
 * @author: xw
 * @create: 2022/9/22 14:50
 * @description: 面试题：模拟银行账户
 *                                      对业务写方法加锁
 *                                      对业务读方法不加锁
 *                                      这样行不行？
 *
 *                                      容易产生脏读问题（dirtyRead）
 */
public class Account {
    String name;
    double balance;

    public synchronized void set(String name,double balance){
        System.out.println("begin");
        this.name = name;
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.balance = balance;
        System.out.println("end");
    }

    public synchronized double getBalance(String name){
        return this.balance;
    }


    public static void main(String[] args) {
        Account a = new Account();
        new Thread(()->a.set("zhangsan",100.0)).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(a.getBalance("zhangsan"));

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(a.getBalance("zhangsan"));
    }
}
